<?php

/**
 *  class File component
 * 
 *  uploads images file handler 
 * 
 * @version 0.1
 * @author Pavlo Rogozhyn <dev.rogozhyn@gmail.com>
 */
class FileImageUpload 
{
    
    //uploads array data
    public $uploads = array();
    
    // CUSTOM 
        
    /**
     *  for validation image mime type
     *  array
     */
    public $image_mime_type = array(
        'image/jpeg', 'image/jpg', 'image/png', 'image/gif'
    );

    
    /**
    * extension file upload
    * 
    * @var mixed
    */
    public $image_ext = array(
        'jpeg', 'jpg', 'png', 'gif'
    );
    
    
    /**
     * 
     * @param type $$upload_array ($this->request->data)
     * @throws Exception
     */
    public function init($upload_array = array())
    {
        try
        {
            
            foreach ($upload_array as $value)
            { 
                if(is_array($value)) 
                {   
                    $this->init($value);
                } else {
                    
                    /*
                     *  $this->uploads - includes
                     * 
                     *  name
                     *  type
                     *  tmp_name
                     *  error
                     *  size
                     *  
                     */
                    $this->uploads = $upload_array;
                }
            }
        } catch(Exception $e){
            echo $e->getMessage(); die;
        }
        
        
    }
    
    
    /**
     * function uploads file
     * 
     * @param $_FILES $request
     * @throws Exception
     * 
     * @return string full_path : full path to  images
     */
    public function upload_process($request, $upload_dir)
    {
        
        $this->init($request);
        
        // debug($this->uploads); die;
            if($upload_dir && (strlen($upload_dir) < 0)) return false;
            
            // tmp path to file
            $file_tmp_path = isset($this->uploads['tmp_name']) ? $this->uploads['tmp_name'] : '';
            
            // directory to upload file
            $target_file_upload = $upload_dir; 
            
            // upload file extension
            $file_extension = strtolower(end(explode(".", $this->uploads['name'])));
            
            // check - validate extension
            if($file_extension && in_array($file_extension, $this->image_ext))
            {
            
                $move_file_name = uniqid();
                $full_path = $upload_dir . $move_file_name . '.' . $file_extension;
                
                try
                {   
                   $r = move_uploaded_file($this->uploads['tmp_name'], $full_path);
                   
                   // if error move file return false
                   if(!$r) return false;
                   
                } catch (Exception $e) {
                    echo $e->getMessage(); die;
                }
                
                return $full_path;

            } else {
                exit('file upload error');
                return false;
            }
    }
    
    
    
    /**
     * 
     */
    public function validate_upload_process($file_error, $file_size = null)
    {
        return ($this->file_error == UPLOAD_ERR_OK && ($this->file_size > 0)) ? TRUE : FALSE;    
    }
    
    /**
     * 
     */
    public function validate_file_extension($file_type)
    {
        
    }
    
    
    public function validate_file_mime_type()
    {
        return in_array($this->file_type, $this->image_mime_type) ? TRUE : FALSE;
    }
    
    /**
     * 
     */
    public function move_file()
    {
        
    }
    
    
    public function copy_file() 
    {

    }

    /**
     * this function deleting file by target ($file)
     * 
     * @param string $file
     * @return boolean
     */
    public function delete_file($file = '') 
    {
        // may isset record in database without images ?
        if (!$file || empty($file) || !(strlen($file) <= 0)) return true;
        
        // unlink file
        if(is_writeable($file) && unlink($file))
        {
            // if unlink file return true
            return true;
        } else if (!file_exists($file)) {
            return true;
        } else {
            return false;
        }
    } // end delete_file method
    
}









